package org.linlinjava.litemall.db.service;

import com.github.pagehelper.PageHelper;
import org.linlinjava.litemall.db.dao.LitemallCreditHistoryMapper;
import org.linlinjava.litemall.db.domain.LitemallCreditHistory;
import org.linlinjava.litemall.db.domain.LitemallCreditHistoryExample;
import org.linlinjava.litemall.db.domain.LitemallSysConfig;
import org.linlinjava.litemall.db.domain.LitemallUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
 * @author: renshuai
 * @date: 2019/3/1 08:38
 * @Description: 积分服务
 */
@Service
public class LitemallCreditHistoryService {

    @Autowired
    private LitemallCreditHistoryMapper creditHistoryMapper;
    @Autowired
    private LitemallUserService userService;

    @Autowired
    private LitemallSysConfigService sysConfigService;

    public int addCreditHistory(LitemallCreditHistory creditHistory) {
        return creditHistoryMapper.insert(creditHistory);
    }


    /**
     * 分页查询
     * @param userId
     * @param page
     * @param size
     * @param sort
     * @param order
     * @return
     */
    public List<LitemallCreditHistory> listByUserId(Integer userId, Integer page, Integer size, String sort, String order) {
        LitemallCreditHistoryExample example = new LitemallCreditHistoryExample();
        example.createCriteria().andUserIdEqualTo(userId);
        if (!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order)) {
            example.setOrderByClause(sort + " " + order);
        }

        PageHelper.startPage(page, size);
        List<LitemallCreditHistory> histories = creditHistoryMapper.selectByExample(example);
        return histories;
    }

    /**
     * 积分转让
     * @param transferUserId
     * @param credit
     * @return
     */
    @Transactional
    public int transfer(Integer userId, Integer transferUserId, BigDecimal credit) {
        //获取转让费
        LitemallSysConfig sysConfig = sysConfigService.getByType(1);
        BigDecimal value = sysConfig.getValue();
        BigDecimal surplus = new BigDecimal(1).subtract(value);
        BigDecimal inCredit = credit.multiply(surplus);
        //该用户增加积分
        LitemallUser tUser = userService.findById(transferUserId);
        //该用户减少积分
        LitemallUser loginUser = userService.findById(userId);

        tUser.setCredit(tUser.getCredit().add(inCredit));
        loginUser.setCredit(loginUser.getCredit().subtract(credit));

        int count = userService.updateById(tUser);
        if (count != 1) {
            return count;
        }

        count = userService.updateById(loginUser);
        if (count != 1) {
            return count;
        }

        LitemallCreditHistory c1 = new LitemallCreditHistory();
        c1.setUserId(userId);
        c1.setCredit(credit);
        c1.setTime(LocalDateTime.now());
        c1.setDescription("转出" + credit + "积分");
        count = creditHistoryMapper.insert(c1);
        if (count != 1) {
            return count;
        }

        LitemallCreditHistory c2 = new LitemallCreditHistory();
        c2.setUserId(transferUserId);
        c2.setCredit(credit);
        c2.setTime(LocalDateTime.now());
        c2.setDescription("向"+loginUser.getMobile()+ "转入" + inCredit + "积分");
        count = creditHistoryMapper.insert(c2);
        if (count != 1) {
            return count;
        }
        return count;

    }
}
